// import { getApi } from '../../services/autoApi';
import { CODE_SUCCESS } from "../../utils/consts";
import {
  required,
  defaultLen,
  phone,
  defaultTextLen,
  numberRule
} from "../../utils/sharedRules";
const API_BASE_URL = require('@/services/baseUrl')
const uploadUrl = `${API_BASE_URL}/oss/uploadFile`;
const defaultRule = [required, defaultLen];
const vehicleAndpartsWarrCalcInputs = function() {
  const inputPlusItems = [
    { fee: this.formModel.otherFee || 0, countType: 1 },
    { fee: this.formModel.hourlyFee || 0, countType: 1 },
    { fee: this.formModel.accessoriesFee || 0, countType: 1 },
    { fee: this.formModel.workRescueTotalCost || 0, countType: 1 },
    { fee: this.formModel.partsCallTotalCost || 0, countType: 1 }
  ];
  return inputPlusItems;
};

const isReasonPartsChange = function change(val, record, index, columnIndex) {
  if (val === 1) {
    if (
      !this.formModel.partsOrderWorkId &&
      this.formModel.workOrderType === "12"
    ) {
      this.$message.warning("请先选择配件订单编号");
      (0, this.dataToSubmit)[index].isReasonParts = undefined;
      return;
    }
    const reasonPartsExist =
      this.dataToSubmit.filter((item) => item.isReasonParts === 1).length > 1;
    if (reasonPartsExist) {
      (0, this.dataToSubmit)[index].isReasonParts = undefined;
      this.$message.warning("最多只能选择一个原因件");
      return;
    }
    (0, this.dataToSubmit)[
      index
    ].partsOrderWorkId = this.formModel.partsOrderWorkId;
  }
};
// const vin = '/workOrder/queryVehicleInfo';
// const repairHisUrl = '/workOrder/queryOrderWarranty';
// function vinSearch() {
//   if (this.formModel.vin.length !== 17) return;

//   // 查询配件信息
//   getApi(vin, { vin: this.formModel.vin }).then((res) => {
//     const { code, message, data } = res.data;
//     if (code === CODE_SUCCESS && data) {
//       const recordKeys = Object.keys(data);
//       Array
//         .prototype
//         .forEach
//         .call(recordKeys, (item) => {
//           const des = Object.getOwnPropertyDescriptor(this.formModel, item);
//           if (des) {
//             (0, this.formModel)[item] = data[item];
//           }
//         });
//       return;
//     }
//     if (code !== CODE_SUCCESS) this.$message.error(message);
//   });
//   // 查询维修记录-此处不需要提交
//   getApi(repairHisUrl, { vin: this.formModel.vin }).then((res) => {
//     const { code, message, data } = res.data;
//     if (code === CODE_SUCCESS && data) {
//       const his = Array
//         .prototype
//         .map
//         .call(data, (item, i) => {
//           const tmp = item;
//           tmp.id = i;
//           return tmp;
//         });
//       this.$set(this.apiData, 'repairHis', his);
//     }
//     if (code !== CODE_SUCCESS) this.$message.error(message);
//   });
//   // 查询工时信息
// }

// todo 这里逻辑有点乱
const zipUploadChange = function handleUpdateList({ file, fileList }) {
  if (!fileList) return;
  this.formModel.workFileList = fileList;
  if (file && file.response) {
    const { code, message } = file.response;
    if (code === CODE_SUCCESS) {
      const { uid, name } = file;
      const url = file.response.data.fileUrl;
      this.formModel.workFileList.pop();
      this.formModel.workFileList.push({
        ...file.response.data,
        uid,
        name,
        url
      });
      this.$message.success(message);
      return;
    }
    this.formModel.workFileList.pop();
    this.$message.error(message);
  }
};

const orderInfo = {
  title: "",
  inputs: [
    {
      label: "工单类型",
      inputName: "workOrderType",
      type: "select",
      dict: "workType",
      value: "1",
      disabled: true,
      toMap: "",
      rules: []
    },
    {
      label: "单据状态",
      inputName: "workState",
      type: "select",
      dict: "workState",
      disabled: true,
      value: "10",
      toMap: "",
      rules: []
    }
  ]
};

const basicInfo = {
  title: "基本信息",
  inputs: [
    {
      label: "强保单编号",
      inputName: "workId",
      value: undefined,
      actionType: ["update", "preview"],
      disabled: true,
      toMap: "",
      rules: defaultRule
    },
    {
      label: "服务站名称",
      inputName: "serviceStationName",
      value: "保定蜂巢旗舰店",
      disabled: true,
      toMap: "",
      rules: defaultRule
    },
    {
      label: "服务站站号",
      inputName: "stationCode",
      value: undefined,
      disabled: true,
      toMap: "",
      rules: defaultRule
    },
    {
      label: "上报人",
      inputName: "reporterName",
      value: undefined,
      toMap: "",
      rules: defaultRule
    },
    {
      label: "服务站联系电话",
      inputName: "serviceStationPhone",
      value: undefined,
      toMap: "",
      rules: [required, phone]
    },
    {
      label: "服务站星级",
      inputName: "serviceStationStar",
      value: "5",
      disabled: true,
      toMap: "",
      rules: []
    },
    {
      label: "上报日期",
      inputName: "reporterDate",
      value: undefined,
      format: "YYYY-MM-DD HH:mm",
      type: "date",
      placeholder: "",
      toMap: "",
      rules: [required]
    }
  ]
};

const userInfo = {
  title: "用户信息",
  inputs: [
    {
      label: "用户类别",
      inputName: "userType",
      type: "select",
      dict: "userType",
      value: undefined,
      toMap: "",
      rules: [required]
    },
    {
      label: "车主姓名",
      inputName: "carOwnerName",
      value: undefined,
      toMap: "",
      rules: [required, defaultLen]
    },
    {
      label: "车主单位",
      inputName: "carOwnerInstitution",
      value: undefined,
      type: "textarea",
      toMap: "",
      rules: [required, defaultTextLen]
    },
    {
      label: "车主联系电话",
      inputName: "carOwnerPhone",
      value: undefined,
      toMap: "",
      rules: [required, phone]
    },
    {
      label: "送修人",
      inputName: "sendPeople",
      value: undefined,
      toMap: "",
      rules: defaultRule
    },
    {
      label: "送修人联系电话",
      inputName: "sendPeoplePhone",
      value: undefined,
      toMap: "",
      rules: [required, phone]
    }
  ]
};

const vehicleInfo = {
  title: "车辆信息",
  inputs: [
    {
      label: "VIN码",
      inputName: "vin",
      value: undefined,
      disabled: true,
      toMap: "",
      rules: [required, { len: 17, message: "请输入长度17位的vin码" }]
    },
    {
      label: "车型",
      inputName: "vehicleModelCode",
      disabled: true,
      value: undefined,
      toMap: "",
      rules: []
    },
    {
      label: "车牌号",
      inputName: "licensePlateNumber",
      value: undefined,
      toMap: "",
      rules: defaultRule
    },
    {
      label: "驱动电机号",
      inputName: "electricEngine",
      disabled: true,
      value: undefined,
      toMap: "",
      rules: []
    },
    {
      label: "驱动电机型号",
      inputName: "electricType",
      disabled: true,
      value: undefined,
      toMap: "",
      rules: []
    },
    {
      label: "行驶里程",
      inputName: "mileage",
      value: undefined,
      toMap: "",
      rules: [required, numberRule]
    },
    {
      label: "生产日期",
      inputName: "produceDate",
      disabled: true,
      value: undefined,
      toMap: "",
      rules: []
    },
    {
      label: "购车日期",
      inputName: "purchaseDate",
      value: undefined,
      type: "date",
      toMap: "",
      rules: [required]
    }
  ]
};

const zipInfo = {
  title: "附件信息",
  inputs: [
    {
      uploadUrl,
      params: { bucket: "WORK_MAINTAIN" },
      label: "上传附件",
      inputName: "workFileList",
      value: [],
      onChange: zipUploadChange,
      type: "uploadFile",
      toMap: "",
      maxNum: 18,
      hooks: {
        afterApiEach(data, key) {
          this.formModel.workFileList = Array.prototype.map.call(
            data[key],
            (item, i) => {
              const { fileName, objectName, fileUrl } = item; // fileDate
              return {
                fileName,
                fileUrl,
                objectName,
                uid: i,
                name: fileName,
                url: fileUrl
              };
            }
          );
        },
        beforeSubmit(key) {
          (0, this.formModel)[key] = Array.prototype.map.call(
            this.formModel.workFileList,
            (item) => {
              // const fileData = item.response ? item.response.data : item;
              const { fileName, objectName, fileUrl } = item; // fileDate
              return { fileName, objectName, fileUrl };
            }
          );
        },
        afterError() {
          this.formModel.workFileList = Array.prototype.map.call(
            this.formModel.workFileList,
            (item, i) => {
              const { fileName, objectName, fileUrl } = item; // fileDate
              return {
                fileName,
                fileUrl,
                objectName,
                uid: i,
                name: fileName,
                url: fileUrl
              };
            }
          );
        }
      },
      rules: [required]
    }
  ]
};
const repairHis = {
  title: "维修记录",
  type: "slot",
  slotName: "repairHis",
  inputs: [
    {
      label: "",
      inputName: "repairHis",
      value: [],
      toMap: "",
      rules: []
    }
  ]
};
const repairHisColumns = [
  {
    title: "序号",
    dataIndex: "id",
    key: "id",
    customRender: (val) => val + 1
  },
  {
    title: "订单编号",
    dataIndex: "workId",
    key: "workId"
  }
];

export {
  orderInfo,
  basicInfo,
  userInfo,
  vehicleInfo,
  zipInfo,
  repairHis,
  repairHisColumns,
  vehicleAndpartsWarrCalcInputs,
  isReasonPartsChange
};
